import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Register from '../components/Register'
import Home from '../components/Home.vue'
import User from '../components/supervisor/user/User.vue'
import Task from '../components/supervisor/task/Task.vue'
import SecondaryTask from '../components/secondary/task/SecondaryTask.vue'
import SecondaryReceiveTask from '../components/secondary/task/SecondaryReceiveTask.vue'
import FulfilledTask from '../components/secondary/task/wiew/FulfilledTask.vue'
import CompletedTask from '../components/secondary/task/wiew/CompletedTask.vue'
import FailTask from '../components/secondary/task/wiew/FailTask.vue'
import SupervisorShop from '../components/supervisor/shop/SupervisorShop.vue'
import ChildTaskInfo from '../components/supervisor/task/ChildTaskInfo'
import SupervisorEmail from '../components/supervisor/message/SupervisorEmail.vue'
import SecondaryShop from '../components/secondary/shop/SecondaryShop'
import SecondaryPackage from '../components/secondary/package/SecondaryPackage'
import SupervisorUnfinishedCommitment from '../components/supervisor/commitment/view/SupervisorUnfinishedCommitment'
import SupervisorCommitmentHome from '../components/supervisor/commitment/SupervisorCommitmentHome'
import SecondaryCommitmentHome from '../components/secondary/commitment/SecondaryCommitmentHome'
import FulfilledCommitment from '../components/secondary/commitment/view/FulfilledCommitment'
import CompletedCommitment from '../components/secondary/commitment/view/CompletedCommitment'
import SupervisorCompletedCommitment from '../components/supervisor/commitment/view/SupervisorCompletedCommitment'
Vue.use(VueRouter)
const routes = [{
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        component: Login
    },
    {
        path: '/register',
        component: Register
    },
    {
        path: '/home',
        component: Home,
        children: [
            { path: '/user', component: User },
            { path: '/task', component: Task },
            { path: '/secondarytask', component: SecondaryTask },
            {
                path: '/secondaryreceivetask',
                component: SecondaryReceiveTask,
                redirect: '/fulfilledtask',
                children: [
                    { path: '/fulfilledtask', component: FulfilledTask },
                    { path: '/completedtask', component: CompletedTask },
                    { path: '/failtask', component: FailTask }
                ]
            },
            { path: '/supervisorshop', component: SupervisorShop },
            { path: '/childtaskinfo', component: ChildTaskInfo },
            { path: '/supervisoremail', component: SupervisorEmail },
            { path: '/secondaryshop', component: SecondaryShop },
            { path: '/secondarypackage', component: SecondaryPackage },
            {
                path: '/supervisorcommitmenthome',
                component: SupervisorCommitmentHome,
                redirect: '/supervisorunfinishedcommitment',
                children: [
                    { path: '/supervisorunfinishedcommitment', component: SupervisorUnfinishedCommitment },
                    { path: '/supervisorcompletedcommitment', component: SupervisorCompletedCommitment },
                ]
            },
            {
                path: '/secondarycommitmenthome',
                component: SecondaryCommitmentHome,
                redirect: '/fulfilledCommitment',
                children: [
                    { path: '/fulfilledCommitment', component: FulfilledCommitment },
                    { path: '/completedCommitment', component: CompletedCommitment },
                ]
            }
        ]

    }

]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    // 访问登录页，放行
    console.log(to.path)
    if (to.path === '/login' || to.path === '/register') return next()
    console.log('获取token')
        // 获取token
    const tokenStr = window.sessionStorage.getItem('token')
        // 没有token, 强制跳转到登录页
    if (!tokenStr) return next('/login')
    next()
})
export default router